/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain an copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.glidebitmappoolsample;

import ohos.aafwk.ability.delegation.AbilityDelegatorRegistry;
import org.junit.Test;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import static org.junit.Assert.assertEquals;

public class ExampleOhosTest {
    /**
     * 包名test
     */
    @Test
    public void testBundleName() {
        final String actualBundleName = AbilityDelegatorRegistry.getArguments().getTestBundleName();
        assertEquals("com.glidebitmappoolsample", actualBundleName);
    }

    /**
     * 正常资源回收机制test
     */
    @Test
    public void releaseBitmapMethod(){
        try {
            Class mainAbilitySlice = Class.forName("com.glidebitmappoolsample.slice.MainAbilitySlice");
            Method releaseBitmapMethod = mainAbilitySlice.getMethod("releaseBitmapMethod");
            Object object = mainAbilitySlice.getConstructor().newInstance();
            releaseBitmapMethod.invoke(object);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
    }

    /**
     * 普通文件回收机制test
     */
    @Test
    public void runNormalFile(){
        try {
            Class mainAbilitySlice = Class.forName("com.glidebitmappoolsample.slice.MainAbilitySlice");
            Method runNormalFile = mainAbilitySlice.getMethod("runNormalFile");
            Object object = mainAbilitySlice.getConstructor().newInstance();
            runNormalFile.invoke(object);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
    }

    /**
     * 资源优化缓存test
     */
    @Test
    public void runResourceOptimized(){
        try {
            Class mainAbilitySlice = Class.forName("com.glidebitmappoolsample.slice.MainAbilitySlice");
            Method runResourceOptimized = mainAbilitySlice.getMethod("runResourceOptimized");
            Object object = mainAbilitySlice.getConstructor().newInstance();
            runResourceOptimized.invoke(object);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
    }

    /**
     * 文件优化缓存test
     */
    @Test
    public void runFileOptimized(){
        try {
            Class mainAbilitySlice = Class.forName("com.glidebitmappoolsample.slice.MainAbilitySlice");
            Method runFileOptimized = mainAbilitySlice.getMethod("runFileOptimized");
            Object object = mainAbilitySlice.getConstructor().newInstance();
            runFileOptimized.invoke(object);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
    }

    /**
     * 文件指定大小优化缓存test
     */
    @Test
    public void runDownSample(){
        try {
            Class mainAbilitySlice = Class.forName("com.glidebitmappoolsample.slice.MainAbilitySlice");
            Method runDownSample = mainAbilitySlice.getMethod("runDownSample");
            Object object = mainAbilitySlice.getConstructor().newInstance();
            runDownSample.invoke(object);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
    }

    /**
     * 清除缓存test
     */
    @Test
    public void clearMemory(){
        try {
            Class mainAbilitySlice = Class.forName("com.glidebitmappoolsample.slice.MainAbilitySlice");
            Method clearMemory = mainAbilitySlice.getMethod("clearMemory");
            Object object = mainAbilitySlice.getConstructor().newInstance();
            clearMemory.invoke(object);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
    }


}